package com.neugh.config.realm;

import com.neugh.entity.User;
import com.neugh.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 *
 * @author LENOVO
 *
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    /**
     * 登录
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userPhone=token.getPrincipal().toString();
        User user=userService.getOne(new QueryWrapper<User>().eq(User.USERPHONE, userPhone));
        if(user==null){
            return null;
        }

        SimpleAuthenticationInfo info =new SimpleAuthenticationInfo(
                userPhone,user.getUserPassword(),getName()
        );
        return info;
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // TODO Auto-generated method stub
        return null;
    }}
